# coding: utf-8
from PySide6.QtCore import QModelIndex
from PySide6.QtGui import QPainter
from PySide6.QtWidgets import QTableView, QStyleOptionViewItem
from qfluentwidgets import SmoothScrollDelegate, TableWidget, TableItemDelegate, setCustomStyleSheet, themeColor


class StyledItemDelegate(TableItemDelegate):
    def _drawIndicator(self, painter: QPainter, option: QStyleOptionViewItem, index: QModelIndex):
        pass

    def _drawBackground(self, painter: QPainter, option: QStyleOptionViewItem, index: QModelIndex):
        """ draw row background """
        r = 5
        if index.column() == 0:
            rect = option.rect.adjusted(0, 0, r + 1, 0)
            painter.drawRoundedRect(rect, r, r)
        elif index.column() == index.model().columnCount(index.parent()) - 1:
            rect = option.rect.adjusted(-r - 1, 0, -4, 0)
            painter.drawRoundedRect(rect, r, r)
        else:
            rect = option.rect.adjusted(-1, 0, 1, 0)
            painter.drawRect(rect)


class TableView(QTableView):
    def __init__(self, parent=None):
        super().__init__(parent)
        self.scrollDelagate = SmoothScrollDelegate(self)
        self.delegate = StyledItemDelegate(self)
        self.setItemDelegate(self.delegate)


class NewTableWidget(TableWidget):
    def __init__(self, parent=None):
        super().__init__(parent)
        self.scrollDelagate = SmoothScrollDelegate(self)
        self.delegate = StyledItemDelegate(self)
        self.setItemDelegate(self.delegate)
        # self.__initQss()

    def __initQss(self):
        r, g, b = themeColor().red(), themeColor().green(), themeColor().blue()
        qss = f'''
        QHeaderView {{
            background-color: transparent;
            border: none;
        }}
        QHeaderView::section {{
            background-color: transparent;
            color: rgba({r}, {g}, {b}, 1);
            padding-left: 5px;
            padding-right: 5px;
            font-size: 20px;
            text-align: center;
        }}
        '''
        setCustomStyleSheet(self, qss, qss)
